﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;

// Adapted from: http://www.codeproject.com/KB/WPF/WPF_Media_Player.aspx

namespace GrooveSharp
{
    public partial class Rating : UserControl
    {
        private IList<CheckBox> m_starList = new List<CheckBox>();

        public static readonly DependencyProperty RatingValueProperty =
                DependencyProperty.Register("RatingValue", typeof(int),
                    typeof(Rating), 
                    new FrameworkPropertyMetadata(new PropertyChangedCallback(OnRatingChanged)));

        public static readonly RoutedEvent RatingChangedEvent =
                  EventManager.RegisterRoutedEvent("RatingChanged", RoutingStrategy.Bubble,
                    typeof(RoutedEventHandler), typeof(MainPlayer));

        public event RoutedEventHandler RatingChanged
        {
            add { AddHandler(RatingChangedEvent, value); }
            remove { RemoveHandler(RatingChangedEvent, value); }
        }

        public Rating()
        {
            InitializeComponent();

            m_starList.Add(star1);
            m_starList.Add(star2);
            m_starList.Add(star3);
            m_starList.Add(star4);
            m_starList.Add(star5);
        }

        private static void OnRatingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Rating rating = sender as Rating;
            rating.UpdateRating(rating.RatingValue);
        }

        // Gets/sets the rating, either 0,20,40,60,80,100
        // Corresponding to 0,1,2,3,4,5 stars
        // So to an index of -1,0,1,2,3,4
        public int RatingValue
        {
            get { return (int)GetValue(RatingValueProperty); }
            set { SetValue(RatingValueProperty, value); }
        }

        private int ToRatingIndex(int? value)
        {
            return value.HasValue ? (value.Value / 20) - 1 : 0;
        }

        private void UpdateRating(int? value)
        {
            App.Log.Debug("Update rating to " + value.ToString());
            int index = 0;
            int ratingIndex = ToRatingIndex(value);
            foreach (CheckBox check in m_starList)
            {
                check.IsChecked = index <= ratingIndex;
                index++;
            }
        }

        private void star_Checked(object sender, RoutedEventArgs e)
        {
            int newRatingIndex = -1;
            if (Int32.TryParse((sender as CheckBox).Tag as string, out newRatingIndex))
            {
                int currentRatingIndex = ToRatingIndex(RatingValue);
                if (newRatingIndex == currentRatingIndex)
                {
                    newRatingIndex--;
                }
                this.RatingValue = (newRatingIndex + 1) * 20;
                App.Log.DebugFormat("Just set rating to {0} (index: {1})", this.RatingValue, newRatingIndex);
            }
            RaiseEvent(new RoutedEventArgs(RatingChangedEvent));
        }

        private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            foreach (CheckBox check in m_starList)
            {
                check.IsEnabled = this.IsEnabled;
            }
        }
    }

}
